<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改房间信息')" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-roomInfo-edit" th:object="${roomInfo}">
        <input name="id" th:field="*{id}" type="hidden">
        <div class="form-group">
            <label class="col-sm-3 control-label">房间图片：</label>
            <div class="col-sm-8">
                <input type="hidden" name="roomPicture" th:field="*{roomPicture}">
                <div class="file-loading">
                    <input class="form-control file-upload" id="roomPicture" name="file" type="file">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">房间对应的公寓ID：</label>
            <div class="col-sm-8">
                <input name="roomId" th:field="*{roomId}" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">房间面积：</label>
            <div class="col-sm-8">
                <input name="area" th:field="*{area}" class="form-control" type="number" step="0.01" min="0.01" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">租金类型：</label>
            <div class="col-sm-8">
                <select name="rentType" class="form-control m-b" th:field="*{rentType}" required>
                    <option value="monthly">月租</option>
                    <option value="quarterly">季度</option>
                    <option value="yearly">年租</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">房间类型：</label>
            <div class="col-sm-8">
                <input name="roomType" th:field="*{roomType}" class="form-control" type="text" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">房东联系方式：</label>
            <div class="col-sm-8">
                <input name="hostPhone" th:field="*{hostPhone}" class="form-control" type="number" required>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: bootstrap-fileinput-js"/>
<script th:inline="javascript">
    var prefix = ctx + "room/roomInfo";

    $("input[name='hostPhone']").on('input', function(e) {
        var input = $(this);
        var value = input.val();

        if (/\D/.test(value)) {
            // 如果包含非数字字符，显示提示信息
            input.addClass('is-invalid');
            if (!input.next('.invalid-feedback').length) {
                input.after('<div class="invalid-feedback">请输入有效的数字</div>');
            }
        } else {
            // 输入都是数字，移除提示
            input.removeClass('is-invalid');
            input.next('.invalid-feedback').remove();

            // 限制长度为11位
            if (value.length > 11) {
                input.val(value.slice(0, 11));
            }
        }
    });

    $("#form-roomInfo-edit").validate({
        focusCleanup: true,
        rules: {
            area: {
                required: true,
                number: true,
                min: 0.01
            },
            hostPhone: {
                required: true,
                digits: true,
                minlength: 11,
                maxlength: 11
            }
        },
        messages: {
            area: {
                required: "请输入房间面积",
                number: "请输入有效的数字",
                min: "房间面积必须大于0"
            },
            hostPhone: {
                required: "请输入房东联系方式",
                digits: "请输入有效的数字",
                minlength: "电话号码必须是11位",
                maxlength: "电话号码必须是11位"
            }
        }
    });

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/edit", $('#form-roomInfo-edit').serialize());
        }
    }

    $(".file-upload").each(function (i) {
        var val = $("input[name='" + this.id + "']").val()
        $(this).fileinput({
            'uploadUrl': ctx + 'common/upload',
            initialPreviewAsData: true,
            initialPreview: [val],
            maxFileCount: 1,
            autoReplace: true
        }).on('fileuploaded', function (event, data, previewId, index) {
            $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
        }).on('fileremoved', function (event, id, index) {
            $("input[name='" + event.currentTarget.id + "']").val('')
        })
        $(this).fileinput('_initFileActions');
    });
</script>
</body>
</html>